//
//  BkCommendsTableViewCell.m
//  TuDianEducation
//
//  Created by lixinkeji on 2021/1/12.
//  Copyright © 2021 zhangbenchao. All rights reserved.
//

#import "BkCommendsTableViewCell.h"
#import "LXSaveUserInforTool.h"

@interface BkCommendsTableViewCell()
@property (nonatomic, strong) UIView *bacView;
@property (nonatomic, strong) UIImageView *headerImg;
@property (nonatomic, strong) UILabel *nameLabel;

@property (nonatomic, strong) UILabel *dateLable;
@property (nonatomic,strong) UILabel *contentLable;

@property (nonatomic,strong) UILabel *oldLabel;
@property (nonatomic,strong) UIButton *detailBtn;
@property (nonatomic,strong) UIButton *likesBtn;

@property (nonatomic,strong) NSMutableArray *mesageUiArray;
@property (nonatomic,strong) NSArray *messageArray;

@end

@implementation BkCommendsTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self viewConfig];
    }
    return self;
}
-(void)viewConfig
{   self.selectionStyle = UITableViewCellSelectionStyleNone;

    [self.contentView addSubview:self.bacView];
    [self.bacView addSubview:self.headerImg];
    [self.bacView addSubview:self.nameLabel];
    [self.bacView addSubview:self.likesBtn];
  
    [self.bacView addSubview:self.contentLable];

    [self.bacView addSubview:self.oldLabel];
    [self.bacView addSubview:self.dateLable];
    [self.bacView addSubview:self.detailBtn];
    
   
    
}

-(UIView *)bacView{
    if (!_bacView) {
        _bacView = [[UIView alloc]initWithFrame:CGRectMake(0, TSScaleW(0), TSScreenWidth , TSScaleW(143))];
        _bacView.backgroundColor = kWhiteColor;
    }
    return _bacView;
}
-(UIImageView *)headerImg{
    if (!_headerImg) {
        _headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(TSScaleW(11), TSScaleW(15), TSScaleW(35), TSScaleW(35))];
        [_headerImg setCornerRadius:_headerImg.height/2.f];
        _headerImg.backgroundColor = kGrayTxtColor;
 
    }
    return _headerImg;
}
-(UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [TSWLTools allocLabel:@"首届星洲" font:systemBoldFont(TSScaleW(13)) textColor:kMainTxtColor frame:CGRectMake(_headerImg.right + TSScaleW(11), _headerImg.top + TSScaleW(12), TSScaleW(300), TSScaleW(13)) textAlignment:(NSTextAlignmentLeft)];
    }
    return _nameLabel;
}

-(UIButton *)likesBtn{
    if (!_likesBtn) {
        _likesBtn = [TSWLTools allocButtonTitle:@"99" font:systemFont(TSScaleW(12)) textColor:kGrayTxtColor image:[UIImage imageNamed:@"点赞_d"] frame:CGRectMake(TSScaleW(289), _headerImg.top, TSScaleW(60), TSScaleW(30)) sel:@selector(likesBtnAction:) taget:self];
        [_likesBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -TSScaleW(5), 0, 0)];
        _likesBtn.right = self.bacView.width;
       [_likesBtn setImage:[[UIImage imageNamed:@"点赞_d"] imageChangeColor:kRedColor] forState:(UIControlStateSelected)];
    }
    return _likesBtn;
}
-(void)likesBtnAction:(UIButton *)sender
{
    !self.likeBlock?:self.likeBlock(sender);
}

-(UILabel *)contentLable{
    if (!_contentLable) {
        _contentLable = [TSWLTools allocLabel:@"一次很好的购物体验，服务周到，物流快速，配送也很周到，卖家态度也很好~ 下次还回购~" font:systemFont(TSScaleW(12)) textColor:kMainTxtColor frame:CGRectMake(TSScaleW(10), TSScaleW(11) + _headerImg.bottom, TSScaleW(355), TSScaleW(42)) textAlignment:(NSTextAlignmentLeft)];
        _contentLable.numberOfLines = 2;
    }
    return _contentLable;
}
-(UILabel *)dateLable{
    if (!_dateLable) {
        _dateLable = [TSWLTools allocLabel:@"1天前" font:systemFont(TSScaleW(11)) textColor:kGrayTxtColor frame:CGRectMake(TSScaleW(15), _contentLable.bottom +TSScaleW(15), TSScaleW(150),TSScaleW(11)) textAlignment:(NSTextAlignmentCenter)];
        _dateLable.cornerRadius = _dateLable.height/2.f;
        _dateLable.backgroundColor = kSubBacColor;
       
    }
    return _dateLable;
}


-(UIButton *)detailBtn{
    if (!_detailBtn) {
        _detailBtn = [TSWLTools allocButtonTitle:@"" font:systemFont(TSScaleW(12)) textColor:kGrayTxtColor image:[UIImage imageNamed:@"更多_d"] frame:CGRectMake(0, TSScaleW(14) + _contentLable.bottom, TSScaleW(60), TSScaleW(30)) sel:@selector(detailBtnAction) taget:self];
        [_detailBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -TSScaleW(5), 0, 0)];
        _detailBtn.right = self.bacView.width;
    }
    return _detailBtn;
}
-(void)detailBtnAction
{
    !self.moreBlock?:self.moreBlock();
}
-(NSMutableArray *)mesageUiArray{
    if (!_mesageUiArray) {
        _mesageUiArray = [NSMutableArray array];
    }
    return _mesageUiArray;
}
-(void)setMessageArray:(NSArray *)messageArray
{
    _messageArray = messageArray;
    for (UIView *v  in self.mesageUiArray) {
        [v removeFromSuperview];
        
    }
    [self.mesageUiArray removeAllObjects];
    CGFloat top = self.dateLable.bottom + TSScaleW(15);
    for (int i = 0; i < _messageArray.count; i++) {
        NSDictionary *dic = _messageArray[i];
        NSString * usertype = dic[@"usertype"];
        NSString *message = [NSString stringWithFormat:@"%@回复%@:%@",usertype.integerValue == 0?dic[@"usernickname"]:@"作者",dic[@"tanickname"],dic[@"content"]];
        UILabel *l = [TSWLTools allocLabel:message font:systemFont(TSScaleW(12)) textColor:kMainTxtColor frame:CGRectMake(_contentLable.left, top, _contentLable.width, TSScaleW(40)) textAlignment:(NSTextAlignmentLeft)];
        l.numberOfLines = 2;
        top +=  TSScaleW(45);
        [self.contentView addSubview:l];
        [self.mesageUiArray addObject:l];
        NSString *string = l.text;
        NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:string];
        NSRange range = [string rangeOfString:@"回复"];
        NSRange range2 = [string rangeOfString:@":"];
        [atts setAttributes:@{NSForegroundColorAttributeName:kGreenColor} range:NSMakeRange(0, range.location)];
        [atts addAttributes:@{NSForegroundColorAttributeName:kBlueColor} range:NSMakeRange(range.location + 2, range2.location - range.location - 1)];
        l.attributedText = atts;
        [l sizeToFit];
    }
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    [_headerImg sd_setImageWithURL:[NSURL URLWithString:_dataDic[@"usericon"]]];
    _nameLabel.text = _dataDic[@"usernickname"];
    NSString *zannum = _dataDic[@"zannum"];
    [self.likesBtn setTitle:zannum forState:(UIControlStateNormal)];
    NSString *iszan = _dataDic[@"iszan"];
    self.likesBtn.selected = iszan.integerValue;
    self.contentLable.text = _dataDic[@"content"];
    self.dateLable.text = _dataDic[@"adtime"];
    
    self.messageArray = _dataDic[@"secondList"];
    
}

@end
